package com.net.server;

import com.alibaba.fastjson.JSON;
import com.pojo.ClientRunTimeInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * @ClassName NettyServerHandler
 * @Description TODO
 * @Author zhuchen216628
 * @Date 2018/9/28 10:51
 * @Version 1.0
 **/
@Component
@Scope("prototype")
public class NettyServerHandler extends SimpleChannelInboundHandler<String> {

    @Autowired
    private ConnectManager connectManager;

    public NettyServerHandler(){
        System.out.println("NettyServerHandler");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        // 收到消息直接打印输出
//        System.out.println(ctx.channel().remoteAddress() + " Say : " + msg);

        // 返回客户端消息 - 我已经接收到了你的消息
//        ctx.writeAndFlush("Received your message !\n");
        ClientRunTimeInfo newInfo = JSON.parseObject(msg,ClientRunTimeInfo.class);

//        ClientRunTimeInfo clientRunTimeInfo = connectManager.getConnectMap().get(ctx.channel());
        connectManager.getConnectMap().put(ctx.channel(),newInfo);

//        ctx.fireChannelRead(msg);
    }

    /*
     *
     * 覆盖 channelActive 方法 在channel被启用的时候触发 (在建立连接的时候)
     *
     * channelActive 和 channelInActive 在后面的内容中讲述，这里先不做详细的描述
     * */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        connectManager.getConnectMap().put(ctx.channel(),new ClientRunTimeInfo());

        System.out.println("RamoteAddress : " + ctx.channel().remoteAddress() + " active !");

        ctx.writeAndFlush( "Welcome to " + InetAddress.getLocalHost().getHostName() + " service!\n");

        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        connectManager.getConnectMap().remove(ctx.channel());
        System.out.println("remove channel：" + ctx.channel());
    }

//    @Override
////    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
////        if (evt instanceof IdleStateEvent) {
////
////            IdleStateEvent event = (IdleStateEvent) evt;
////
////            if (event.state().equals(IdleState.READER_IDLE)) {
////                //未进行读操作
////                System.out.println("READER_IDLE");
////                // 超时关闭channel
////                ctx.close();
////
////            } else if (event.state().equals(IdleState.WRITER_IDLE)) {
////                System.out.println("WRITER_IDLE");
////
////            } else if (event.state().equals(IdleState.ALL_IDLE)) {
////                //未进行读写
////                System.out.println("ALL_IDLE");
////                // 发送心跳消息
//////                ctx.writeAndFlush("heartbeat!!!!!!!!!!\n");
////                ctx.writeAndFlush( "heart beat \r\n");
////            }
////
////        }
////    }
}
